package com.itheima.reggie.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private CategoryMapper categoryMapper;



    //删除菜品
    @Override
    public int deleteById(Long id)  {
        LambdaQueryWrapper<Dish> QueryWrapper = new LambdaQueryWrapper<>();
        QueryWrapper.eq(Dish::getCategoryId, id);
        Integer count = dishMapper.selectCount(QueryWrapper);
        if (count > 0) {
          throw new CustomException("当前菜品联系其他,不能删除");
        }
        LambdaQueryWrapper<Setmeal> setmealQueryWrapper = new LambdaQueryWrapper<>();
        setmealQueryWrapper.eq(Setmeal::getCategoryId, id);
        int count1 = setmealMapper.selectCount(setmealQueryWrapper);
        if (count1 > 0) {
            throw new CustomException("当前套餐联系其他,不能删除");
        }
        int i = categoryMapper.deleteById(id);
        return i;
    }
}
